/*1.题目描述：线程A打印-我是线程A；线程B打印-我是线程B； 最终实现交替打印，不能出现连续的相同打印。*/
#include <iostream>
#include <pthread.h>

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

void* thread(void* argc)
{

    for (int i = 0; i < 5; ++i)
    {
        pthread_mutex_lock(&mtx);
        pthread_cond_wait(&cond, &mtx);
        std::cout << (char*)argc << std::endl;
        pthread_mutex_unlock(&mtx);
        pthread_cond_signal(&cond);
    }
    return nullptr;
}

int main()
{
    pthread_t ptid[2];
    pthread_create(ptid, nullptr, thread, (void*)"我是线程A");
    pthread_create(ptid + 1, nullptr, thread, (void*)"我是线程B");
    std::cout << "开始" << '\n';
    pthread_cond_signal(&cond);

    int n1 = pthread_join(ptid[0], nullptr);
    int n2 = pthread_join(ptid[1], nullptr);
    if (n1) perror("join1");
    if (n2) perror("join2");

    pthread_cond_destroy(&cond);
    pthread_mutex_destroy(&mtx);
    return 0;
}